<?php
//-------------------------------------------------------------------
//Nazov:		GetDefaultMailText
//Popis:		Nacitanie preddefinovaneho textu z DB pre prihlaseneho admina
function GetDefaultMailText($Admin_ID){
	$sql_result = mysql_query("SELECT Email_Text FROM email_default_values WHERE Admin_ID=$Admin_ID");
	$fetch_row_result = mysql_fetch_row($sql_result);
	
	if($fetch_row_result[0]!="")
		return $fetch_row_result[0];
	else
		return   PL_DEFAULT_EMAIL_TEXT_PART1
				.$_SERVER["HTTP_HOST"]
				.PL_DEFAULT_EMAIL_TEXT_PART2;
	
}
//-------------------------------------------------------------------


//-------------------------------------------------------------------
//Nazov:		GetDefaultMailTextDown
//Popis:		Nacitanie preddefinovaneho textu paty z DB pre prihlaseneho admina
function GetDefaultMailTextDown($Admin_ID){
	$sql_result = mysql_query("SELECT Email_Text_down FROM email_default_values WHERE Admin_ID=$Admin_ID");
	$fetch_row_result = mysql_fetch_row($sql_result);
	
	if($fetch_row_result[0]!="")
		return $fetch_row_result[0];
	else
		return PL_DEFAULT_EMAIL_TEXT_PART3;
	
}
//-------------------------------------------------------------------


//-------------------------------------------------------------------
//Nazov:		SetDefaultMailText
//Popis:		Nastavenie preddefinovaneho textu z DB pre prihlaseneho admina
function SetDefaultMailText($Admin_ID,$Email_Text){
	$sql_result = mysql_query("SELECT COUNT(*) FROM email_default_values WHERE Admin_ID=$Admin_ID");
	$fetch_row_result = mysql_fetch_row($sql_result);
	
	if($fetch_row_result[0]==0)
		mysql_query("	INSERT INTO email_default_values (Admin_ID,Email_Text)
						VALUES($Admin_ID,'$Email_Text')");
	else
		mysql_query("	UPDATE 	email_default_values
						SET 	Email_Text = '$Email_Text'
						WHERE 	Admin_ID = $Admin_ID");
}
//-------------------------------------------------------------------


//-------------------------------------------------------------------
//Nazov:		SetDefaultMailTextDown
//Popis:		Nastavenie preddefinovaneho textu z DB pre prihlaseneho admina
function SetDefaultMailTextDown($Admin_ID,$Email_Text_down){
  	$sql_result = mysql_query("SELECT COUNT(*) FROM email_default_values WHERE Admin_ID=$Admin_ID");
	$fetch_row_result = mysql_fetch_row($sql_result);
	
	if($fetch_row_result[0]==0){
		mysql_query("	INSERT INTO email_default_values (Admin_ID,Email_Text_down)
						VALUES($Admin_ID,'$Email_Text_down')");
	}
	else{
		mysql_query("	UPDATE 	email_default_values
						SET 	Email_Text_down = '$Email_Text_down'
						WHERE 	Admin_ID = $Admin_ID");
	}
}
//-------------------------------------------------------------------


//-------------------------------------------------------------------
//Nazov:		Insert_Product_To_Cookie
//Popis:		Vytvorenie noveho zaznamu v Cookie
function Save_Shared_Products_From_Cookie_To_DB($DB_Product_ID){
	if ((isset($_COOKIE[SHARED_PRODUCTS_COOKIE])) and ($DB_Product_ID!="") and ($DB_Product_ID!=0)){
	    $CookieValue = $_COOKIE[SHARED_PRODUCTS_COOKIE];
	    if ($CookieValue!=""){
	      	$Arr = explode("-",$CookieValue);
	      	$Arr_Count = count($Arr);
	      	for($i=0;$i<$Arr_Count;$i++)
		    	if(!Product_Is_Insert($Arr[$i],$DB_Product_ID)){
					mysql_query("INSERT INTO shared_products (Product_ID,Shared_Product_ID) VALUES ($DB_Product_ID,".$Arr[$i].")");
				}
	    }
	}
}
//-------------------------------------------------------------------

//-------------------------------------------------------------------
//Nazov:		Insert_Product_To_Cookie
//Popis:		Vytvorenie noveho zaznamu v Cookie
function Unset_Shared_Products_Cookie(){
	if (isset($_COOKIE[SHARED_PRODUCTS_COOKIE])){
		$a = "-";
		return setcookie(SHARED_PRODUCTS_COOKIE,$a,time() - 3600);
	}
	print " cookie ".SHARED_PRODUCTS_COOKIE." - neexistuje ";
}
//-------------------------------------------------------------------


//-------------------------------------------------------------------
//Nazov:		Insert_Product_To_Cookie
//Popis:		Vytvorenie noveho zaznamu v Cookie
function Insert_Product_To_Cookie($Cookie_Value, $ProductID){
	if(!Product_Is_Insert_In_Cookie($Cookie_Value, $ProductID)){
		if($Cookie_Value=="")
			$Cookie_Value = $ProductID;
		else
			$Cookie_Value = $Cookie_Value."-".$ProductID;
		return Defragment_Arr($Cookie_Value);
	}
	else{
	  	print  "<script language=\"JavaScript\">
	                <!--
	                        window.alert(\"".PL_TENTO_PRODUKT_JE_UZ_VYBRANY."\");
	                //-->
	            </script>";
		return Defragment_Arr($Cookie_Value);
	}
}
//-------------------------------------------------------------------


//-------------------------------------------------------------------
//Nazov:		Delete_Product_From_Cookie
//Popis:		Odstranenie zaznamu z Cookie
function Delete_Product_From_Cookie($CookieValue, $Product_ID){
	$Arr = explode("-",$CookieValue);
	$Arr_Count = count($Arr);
	
	$Delete_Index = Get_Index($CookieValue,$Product_ID);
	
	if(($Delete_Index!=-1) and ($Delete_Index<$Arr_Count)){
		$Arr[$Delete_Index] = "";		
		return Defragment_Arr(implode("-",$Arr));
	}
	else
		return $CookieValue;
}
//-------------------------------------------------------------------

//-------------------------------------------------------------------
//Nazov:		Generate_Select_SQL
//Popis:		Vytvorenie SQL zo zoznamu v Cookie
//-------------------------------------------------------------------
function Generate_Select_SQL_From_Cookie($Cookie_Value){
	if ($Cookie_Value!=""){
		$Arr = explode("-",$Cookie_Value);
		$Arr_Count = count($Arr);
			
		if($Arr_Count<1) return "";
		
		for($i=0;$i<$Arr_Count;$i++)
			if($Arr[$i]!="")
				$where .= "(ID=".$Arr[$i].") OR ";
		
		$where = substr($where,0,-4);	
		return "SELECT * FROM products WHERE ($where)";
	}
	else
		return "";
};

function Get_Index($Arr_String,$Search_String){
	$Arr = explode("-",$Arr_String);
  	$Arr_Count = count($Arr);
  	for($i=0;$i<$Arr_Count;$i++){
  		if($Arr[$i]==$Search_String) return $i;  		
  	}
  	return -1;
}

function Product_Is_Insert_In_Cookie($Cookie_Value, $Product_ID){
	$Arr = explode("-",$Cookie_Value);
  	$Arr_Count = count($Arr);
  	for($i=0;$i<$Arr_Count;$i++){
  		if($Arr[$i]==$Product_ID) return true;  		
  	}
  	return false;
}

//-------------------------------------------------------------------
//Nazov:		Defragment_Arr
//Popis:		defragmentacia pola
function Defragment_Arr($String){
	$Arr = explode("-",$String);
	$Arr_count = count($Arr);
	$i=0;
	while($i<($Arr_count-1)){
	  	$ii=$i;
		while(($Arr[$i]=="") and ($ii<($Arr_count-1))){
		  	//print $Arr[$i];
			for($j=$i;$j<($Arr_count-1);$j++){
				$Arr[$j]=$Arr[$j+1];
			}
			$Arr[$j]="";
			$ii++;
		}
		$i++;
	}
	//unset
	for($i=0;$i<($Arr_count);$i++)
	  	if($Arr[$i]=="")
			unset($Arr[$i]);
	return implode("-",$Arr);
}
//-------------------------------------------------------------------

function SendMail($Products_List,$Address_List,$Email_Text,$EMail_Text_Down,$Email_Subject,$Email_Sender){

    function getProductURL($Products_List, $Email_Text, $EMail_Text_Down){
        $PArray = explode('-',$Products_List);
        $_URL = new classURL();
        $_PSRV= new classProductService();
        $_PPRC= new classPriceExport();

        $result = "Content-Type: text/html; charset=UTF-8\r\n";
        $result.= "Content-Transfer-Encoding: 8bit\r\n";
        $result.= '
            <html>
                <head>
                    <style  type="text/css">
                        td{
                            font-family:  verdana;
                            font-size: 10px;
                            border-bottom:1px dotted gray;
                            vertical-align:top;
                        }
                    </style>
                </head>
                <body>
                    <table>
                        <tr>
                            <td colspan="2" style="border:0px;">
                                '.$Email_Text.'
                            </td>
                        </tr>
        ';
        foreach ($PArray as $ProductID){
            $PInfo = $_PSRV->getProductInfo($ProductID);
            $href  = 'http://'.$_SERVER['SERVER_NAME']._SYSTEM_SUBDIRS_.'/'.$_URL->SetURL('keep_value', 'name'._URL_VAL_DELIMITER_.$PInfo['SC_ID']._URL_VAL_DELIMITER_.$ProductID._URL_VAL_DELIMITER_.'0', 'keep_value');
            $result .= '
                        <tr>
                            <td>
                                <br>
                                <a href='.$href.' target="_blank">
                                    <img src="http://'.$_SERVER['SERVER_NAME']._SYSTEM_SUBDIRS_.'/'._PRODUCTS_IMAGE_DIR_.'/'.$ProductID._PREVIEW_IMAGE_SUFFIX_.'.'.'jpg">
                                </a>
                                <br>
                                <b>'.$_PPRC->simplePrice($ProductID, 1).'</b>
                            </td>
                            <td>
                                <br>
                                <a href='.$href.' target="_blank"><b>'.$PInfo['Product_Name'].'</b></a>
                                <br>
                                <br>
                                <a href='.$href.' target="_blank">'.substr($PInfo['Product_Desc'],0,_TEXTS_LENGHT_IN_PREVIEW_).'...</a>
                            </td>
                        </tr>
            ';
            
        }
        $result .= '
                        <tr>
                            <td colspan="2" style="border:0px;">
                                <br>
                                '.$EMail_Text_Down.'
                            </td>
                        </tr>
                    </table>
                </body>
            </html>
        ';

        return $result;
    }

    // To send HTML mail, the Content-type header must be set
    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
    // Additional headers
    $headers .= 'From: '.$Email_Sender. "\r\n";
    $headers .= 'Reply-To: '.$Email_Sender.'\r\n';
    $headers .= 'Bcc: '.$Address_List. "\r\n";


	mail(	'',
			$Email_Subject,
			getProductURL($Products_List, $Email_Text, $EMail_Text_Down),
			$headers);
}

//-------------------------------------------------------------------
//Nazov:		Defragment_Arr
//Popis:		defragmentacia pola
function CheckAddressList($Addr_List){
	$Arr = explode(",",$Addr_List);
	$Arr_count = count($Arr);
	$i=0;
	while($i<($Arr_count-1)){
	  	$ii=$i;
		while(($Arr[$i]=="") and ($ii<($Arr_count-1))){
		  	//print $Arr[$i];
			for($j=$i;$j<($Arr_count-1);$j++){
				$Arr[$j]=$Arr[$j+1];
			}
			$Arr[$j]="";
			$ii++;
		}
		$i++;
	}
	//unset
	for($i=0;$i<($Arr_count);$i++)
	  	if($Arr[$i]=="")
			unset($Arr[$i]);
	return implode(",",$Arr);
}
//-------------------------------------------------------------------

//-------------------------------------------------------------------
//Nazov:		Adm_Show_Send_Mail_Button
//Popis:		zobrazenie komponentu pre editovanie e-mail textu paty
function Adm_Show_Send_Mail_Button(){
	print "
		<form action=\"adm_show_selected_products.php\" target=\"selected\" method=\"post\">
			<input type=\"hidden\" name=\"Send_Mail\" value=\"YES\" />
            <table>
                <tr>
                    <td>
                        ".PL_EMAIL_ADRESA_ODOSIELATELA."
                    </td>
                    <td>
                        <input type=\"text\" name=\"Mail_AddressOfSender\" value=\"\" />
                    </td>
                </tr>
                    <td>
                        ".PL_PREDMET_EMAILU."
                    </td>
                    <td>
                        <input type=\"text\" name=\"Mail_Subject\" value=\"\" />
                    </td>
                </tr>
                <tr>
                    <td colspan=\"2\">
                        <input type=\"submit\" value=\"".PL_ODOSLAT_EMAIL."\" style=\"width:320px;\" />
                    </td>
                </tr>
		</form>
	";
}
//-------------------------------------------------------------------


function _br2nl($Text){  //--opacna funkcia je funkciou PHP
	return str_replace(0x0A,"<br />",$Text);
}
function _nl2br($Text){  //--opacna funkcia je funkciou PHP
	return str_replace(0x0A,"<br />",$Text);//($Text,"<br />",0x0A);
}

//-------------------------------------------------------------------
//Nazov:		Adm_Show_EMail_Text_Down
//Popis:		
function Adm_Show_EMail_Text_Down($EMail_Text_Down){
	print "
		<table  style=\"width:320px;border:0px solid black;\">
			<tr style=\"background-color:".TR_BACKGOUND_COLOR_2.";\">
				<td>
					<b>".PL_PATA_EMAILU."</b>
				</td>
			</tr>
			<tr>
				<td>
					<textarea name=\"EMail_Text_Down\" wrap readonly style=\"width:320px;height:50px;background-color:".TR_BACKGOUND_COLOR_1.";\"
						onClick=\"javascript:window.open('adm_compose_mail_text_down.php','adm_compose_text_down','width=450px,height=180px,location=no,directories=no,menubar=no,resizeable=yes,status=no,toolbar=no,scrollbars=yes');\"
					>$EMail_Text_Down</textarea>
				</td>
			</tr>
			<tr>
				<td>
					<input type=\"button\" value=\"".PL_UPRAVIT_PATU_EMAILU."\" style=\"width:100%;\"
						onClick=\"javascript:window.open('adm_compose_mail_text_down.php','adm_compose_text_down','width=450px,height=180px,location=no,directories=no,menubar=no,resizeable=yes,status=no,toolbar=no,scrollbars=yes');\"
					/>
				</td>
			</tr>
		</table>
	";
}
//-------------------------------------------------------------------

//-------------------------------------------------------------------
//Nazov:		Adm_Show_EMail_Text
//Popis:		zobrazenie komponentu pre editovanie e-mail text
function Adm_Show_EMail_Text($EMail_Text){
	print "
		<table  style=\"width:320px;border:0px solid black;\">
			<tr style=\"background-color:".TR_BACKGOUND_COLOR_2.";\">
				<td>
					<b>".PL_UPRAVIT_DOPLNUJUCI_TEXT."</b>
				</td>
			</tr>
			<tr>
				<td>
					<textarea name=\"Text\" wrap readonly style=\"width:320px;height:80px;background-color:".TR_BACKGOUND_COLOR_1.";\"
					onClick=\"javascript:window.open('adm_compose_mail_text.php','adm_compose_text','width=450px,height=240px,location=no,directories=no,menubar=no,resizeable=yes,status=no,toolbar=no,scrollbars=yes');\"
					>$EMail_Text</textarea>
				</td>
			</tr>
			<tr>
				<td>
					<input type=\"button\" value=\"".PL_UPRAVIT_TEXT_EMAILU."\" style=\"width:100%;\"
						onClick=\"javascript:window.open('adm_compose_mail_text.php','adm_compose_text','width=450px,height=240px,location=no,directories=no,menubar=no,resizeable=yes,status=no,toolbar=no,scrollbars=yes');\"
					/>
				</td>
			</tr>
		</table>
	";
}
//-------------------------------------------------------------------


//-------------------------------------------------------------------
//Nazov:		Adm_Update_Address_List
//Popis:		zobrazenie komponentu pre editovanie e-mail text
function Adm_Update_Address_List($Address_List){
	print "
		<table  style=\"width:320px;border:0px solid black;\">
			<tr style=\"background-color:".TR_BACKGOUND_COLOR_2.";\">
				<td>
					<b>".PL_ZOZNAM_ADRIES."</b>
				</td>
			</tr>
			<tr>
				<td>
					<textarea name=\"Text\" wrap readonly style=\"width:320px;height:50px;background-color:".TR_BACKGOUND_COLOR_1.";\"
						onClick=\"javascript:window.open('adm_compose_mail_address_main.php','adm_compose_mail_address_main','width=720px,height=430px,location=no,directories=no,menubar=no,resizeable=yes,status=no,toolbar=no,scrollbars=yes');\"
					>$Address_List</textarea>
				</td>
			</tr>
			<tr>
				<td>
					<input type=\"button\" value=\"".PL_UPRAVIT_ZOZNAM_ADRIES."\" style=\"width:100%;\"
						onClick=\"javascript:window.open('adm_compose_mail_address_main.php','adm_compose_mail_address_main','width=720px,height=430px,location=no,directories=no,menubar=no,resizeable=yes,status=no,toolbar=no,scrollbars=yes');\"		
					/>
				</td>
			</tr>
		</table>
	";
}
//-------------------------------------------------------------------

function SendForm($Product_List){
	print "
		<form action=\"adm_compose_mail\" target=\"adm_compose_mail\" method=\"post\">
			<input type=\"hidden\" name=\"product_list\" value=\"$Product_List\">	
			<input type=\"submit\" value=\"".PL_ODOSLAT_PRODUKTY."\">
		</form>
	";
}

function Adm_Show_Products_From_Cookie($CookieValue, $ABSOLUTE_SERVER_REMOTE_ADDR){
  
	do{
		$sql = Generate_Select_SQL_From_Cookie($CookieValue);
		if ($sql=="") break;
	  	
		print "
			<table style=\"width:320px;border:0px solid black;\">
				<tr style=\"background-color:".TR_BACKGOUND_COLOR_2.";\">
					<td>
						<b>".PL_KOD_PRODUKTU."</b>
					</td>
					<td colspan=\"2\">
						<b>".PL_NAZOV_PRODUKTU."</b>
					</td>
				</tr>
				";
		
		$sql_result=mysql_query($sql);
		while($sql_array = mysql_fetch_object($sql_result)){
			 print"
			 	<tr style=\"background-color:".TR_BACKGOUND_COLOR_1.";\"
				 	onmouseover=\"this.style.backgroundColor='".BUTTON_ON_OVER."';\"
					onmouseout=\"this.style.backgroundColor='".TR_BACKGOUND_COLOR_1."';\" />
			 		<td>
			 			<b><font color=\"black\"><small>

                            <a
                                href=\"javascript:\"
                                target=\"rp_product_detail\"
                                onClick=\"OpenPage(".$sql_array->ID.",1);\"
                            >[".PL_KOD.": ".$sql_array->Product_Code."]"."</a>

						</small></font color></b>
			 		</td>
			 		<td>

                        <a
                            href=\"javascript:\"
							target=\"rp_product_detail\"
                            onClick=\"OpenPage(".$sql_array->ID.",1);\"
                        >".$sql_array->Product_Name."</a>

			 		</td>
			 		<td align=\"right\">
			 			<form action=\"adm_show_selected_products.php\" target=\"selected\">
			 				<input type=\"hidden\" name=\"Delete_Product_ID\" value=\"".$sql_array->ID."\" />
			 				<input type=\"hidden\" value=\"".$_REQUEST['ProductID']."\" name=\"ProductID\" />
			 				<input type=\"submit\" value=\"x\"
								style=\"width:22px;height:15px;\"
								onmouseover=\"this.style.backgroundColor='".BUTTON_ON_OVER."'; this.style.fontWeight='bolder';\"
								onmouseout=\"this.style.backgroundColor='white'; this.style.fontWeight='normal';\" />
			 			</form>
					 </td>
			 	</tr>
			 ";
		}
		print "</table>";
	}while (false);
}
?>